<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品编号" prop="goodId">
      <el-input v-model="dataForm.goodId" placeholder="商品编号"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodName">
      <el-input v-model="dataForm.goodName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="" prop="seckillSort">
      <el-input v-model="dataForm.seckillSort" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="goodImg">
      <el-input v-model="dataForm.goodImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="goodPrice">
      <el-input v-model="dataForm.goodPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="秒杀价格" prop="seckillPrice">
      <el-input v-model="dataForm.seckillPrice" placeholder="秒杀价格"></el-input>
    </el-form-item>
    <el-form-item label="秒杀开始时间" prop="seckillStarttime">
      <el-input v-model="dataForm.seckillStarttime" placeholder="秒杀开始时间"></el-input>
    </el-form-item>
    <el-form-item label="秒杀结束时间" prop="seckillEndtime">
      <el-input v-model="dataForm.seckillEndtime" placeholder="秒杀结束时间"></el-input>
    </el-form-item>
    <el-form-item label="秒杀状态" prop="seckillStatus">
      <el-input v-model="dataForm.seckillStatus" placeholder="秒杀状态"></el-input>
    </el-form-item>
    <el-form-item label="秒杀数量" prop="seckillNum">
      <el-input v-model="dataForm.seckillNum" placeholder="秒杀数量"></el-input>
    </el-form-item>
    <el-form-item label="" prop="a">
      <el-input v-model="dataForm.a" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="b">
      <el-input v-model="dataForm.b" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="c">
      <el-input v-model="dataForm.c" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
          baseURL: 'http://localhost:7001',
        visible: false,
        dataForm: {
          id: 0,
          goodId: '',
          goodName: '',
          seckillSort: '',
          goodImg: '',
          goodPrice: '',
          seckillPrice: '',
          seckillStarttime: '',
          seckillEndtime: '',
          seckillStatus: '',
          seckillNum: '',
          a: '',
          b: '',
          c: ''
        },
        dataRule: {
          goodId: [
            { required: true, message: '商品编号不能为空', trigger: 'blur' }
          ],
          goodName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          seckillSort: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          goodPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          seckillPrice: [
            { required: true, message: '秒杀价格不能为空', trigger: 'blur' }
          ],
          seckillStarttime: [
            { required: true, message: '秒杀开始时间不能为空', trigger: 'blur' }
          ],
          seckillEndtime: [
            { required: true, message: '秒杀结束时间不能为空', trigger: 'blur' }
          ],
          seckillStatus: [
            { required: true, message: '秒杀状态不能为空', trigger: 'blur' }
          ],
          seckillNum: [
            { required: true, message: '秒杀数量不能为空', trigger: 'blur' }
          ],
          a: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          b: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          c: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
              axios({
              url: `${this.baseURL}/hospital_goods/seckilllist/info/${this.dataForm.id}`,
              method: 'get',
              params: '',
              // `responseType` 表示浏览器将要响应的数据类型
              // 选项包括: 'arraybuffer', 'document', 'json', 'text', 'stream'
              responseType: 'json'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodId = data.seckillList.goodId
                this.dataForm.goodName = data.seckillList.goodName
                this.dataForm.seckillSort = data.seckillList.seckillSort
                this.dataForm.goodImg = data.seckillList.goodImg
                this.dataForm.goodPrice = data.seckillList.goodPrice
                this.dataForm.seckillPrice = data.seckillList.seckillPrice
                this.dataForm.seckillStarttime = data.seckillList.seckillStarttime
                this.dataForm.seckillEndtime = data.seckillList.seckillEndtime
                this.dataForm.seckillStatus = data.seckillList.seckillStatus
                this.dataForm.seckillNum = data.seckillList.seckillNum
                this.dataForm.a = data.seckillList.a
                this.dataForm.b = data.seckillList.b
                this.dataForm.c = data.seckillList.c
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            axios({
              url: `${this.baseURL}/hospital_goods/seckilllist/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'goodId': this.dataForm.goodId,
                'goodName': this.dataForm.goodName,
                'seckillSort': this.dataForm.seckillSort,
                'goodImg': this.dataForm.goodImg,
                'goodPrice': this.dataForm.goodPrice,
                'seckillPrice': this.dataForm.seckillPrice,
                'seckillStarttime': this.dataForm.seckillStarttime,
                'seckillEndtime': this.dataForm.seckillEndtime,
                'seckillStatus': this.dataForm.seckillStatus,
                'seckillNum': this.dataForm.seckillNum,
                'a': this.dataForm.a,
                'b': this.dataForm.b,
                'c': this.dataForm.c
              },
            // `responseType` 表示浏览器将要响应的数据类型
            // 选项包括: 'arraybuffer', 'document', 'json', 'text', 'stream'
            responseType: 'json'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
